<?php
function setWidthHeight($width,$height,$maxWidth,$maxHeight){
	$ret=array($width,$height);
	$rat=$width/$height;
	if($width>$maxWidth || $height>$maxHeight){
		$ret[0]=$maxWidth;
		$ret[1]=$ret[0]/$rat;
		if($ret[1]>$maxHeight){
			$ret[1]=$maxHeight;
			$ret[0]=$ret[1]*$rat;
		}
	}
	return $ret;
}
//修改图像大小的函数
function createthumb($img,$size='s'){
	if(is_file($img)){//检查是否是文件
		if($cursize=getimagesize($img)){//获取文件大小
			$sizes=array('s'=>100,'m'=>300,'l'=>600);
			if(!array_key_exists($size,$sizes))
				$size='s';
				$newsize=setWidthHeight($cursize[0],$cursize[1],$sizes[$size],$sizes[$size]);
				$thepath=pathinfo($img);
				$dst=imagecreatetruecolor($newsize[0],$newsize[1]);
				$filename=str_replace('.'.$thepath['extension'],'',$img);
				$filename=$filename.'_th'.$size.'.'.$thepath['extension'];
				$types=array('jpg'=>array('imagecreatefromjpeg','imagejpeg'),
				'jpeg'=>array('imagecreatefromjpeg','imagejpeg'),
				'gif'=>array('imagecreatefromgif','imagegif'),
				'png'=>array('imagecreatefrompng','imagepng'));
				$func=$types[$thepath['extension']][0];
				$src=$func($img);
				//创建拷贝
				imagecopyresampled($dst,$src,0,0,0,0,$newsize[0],$newsize[1],$cursize[0],$cursize[1]);
				//创建缩略图
				$func=$types[$thepath['extension']][1];
				$func($dst,$filename);
?>
				<img src="<?php echo $filename;?>">
<p>
<a href="thumb.php?img=<?php echo $img;?>&amp;sml=s" onclick="changesize('<?php echo $img;?>','s');return false;">小图</a>
<a href="thumb.php?img=<?php echo $img;?>&amp;sml=m" onclick="changesize('<?php echo $img;?>','m');return false;">中图</a>
<a href="thumb.php?img=<?php echo $img;?>&amp;sml=l" onclick="changesize('<?php echo $img;?>','l');return false;">大图</a>
</p>
<?php
				return;
				
		}
	}
	echo "没有图像";
}
createthumb($_GET['img'],$_GET['sml']);
?>